package com.example.demo.demos.现场调查;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import com.example.demo.demos.web.DetailData;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.ArrayList;
import java.util.List;

/**
 * 非浓度或强度稳定岗位的详细劳动者接触情况
 *
 * @author Sayi
 */
public class LdjcsTablePolicy extends DynamicTableRenderPolicy {

    // 人工费填充数据所在行数
    int startRow = 1;

    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        if (null == data) return;
        @SuppressWarnings("unchecked")
        List<RowRenderData> list = (List<RowRenderData>) data;
        // 创建新的 ArrayList 并添加元素
        ArrayList<RowRenderData> ldjcs = new ArrayList<>(list);
        if (null != ldjcs) {
            table.removeRow(startRow);
            for (int i = ldjcs.size() - 1; i >= 0; i--) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(startRow);
                for (int j = 0; j < 9; j++) insertNewTableRow.createCell();
                // 倒序的 也就是最后一行渲染完毕后开始合并单元格
                if (i == 0) {
                    TableTools.mergeCellsVertically(table, 0, startRow, startRow + 1);

                    TableTools.mergeCellsVertically(table, 2, startRow , startRow + 1);

//                    TableTools.mergeCellsVertically(table, 0, startRow + 4, startRow + 5);
//
//                    TableTools.mergeCellsVertically(table, 1, startRow + 5, startRow + 6);
//                    TableTools.mergeCellsVertically(table, 2, startRow + 1, startRow + 3);
                }
                System.out.println("insertNewTableRow = " + (ldjcs.get(i)));
                TableRenderPolicy.Helper.renderRow(table.getRow(startRow), ldjcs.get(i));
            }
        }
    }

}
